package com.kecheng.util;


import java.util.ArrayList;
import java.util.List;

import com.kecheng.dao.PeizhiDao;
import net.sf.json.JSONObject;
import com.kecheng.pojo.Peizhi;
import com.kecheng.pojo.Tag;

import com.kecheng.com.yx.pojo.AccessToken;
import com.kecheng.com.yx.pojo.BaseResult;
import com.kecheng.com.yx.pojo.TagToTeam;
import com.kecheng.com.yx.util.WeixinUtil;

import com.kecheng.dao.CommonDao;


/**
 * 创建标签接口
 * @author Administrator
 *
 */
public class CreateTags {
	private static PeizhiDao peizhiDao = (PeizhiDao) SpringUtil.getBean("peizhiDao");
	private static Peizhi peizhi = peizhiDao.find();
	private static CommonDao dao = (CommonDao) SpringUtil.getBean("dao");
	
	/**
	 * 批量为用户打上标签
	 */
	public static void addTagToTeams(List<String>openidList,Integer tagId){
		AccessToken at = WeixinUtil.getAccessToken();
		String url="https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", at.getToken());
		
		TagToTeam ttt = new TagToTeam();
		ttt.setOpenid_list(openidList);
		ttt.setTagid(tagId);
		String jsonData = JSONObject.fromObject(ttt).toString();
		JSONObject jsonobject = WeixinUtil.httpRequest(url, "POST", jsonData);
		
		//{"errmsg":"ok","errcode":0}
		if(jsonobject!=null&&"ok".equals(jsonobject.getString("errmsg"))&&jsonobject.getInt("errcode")==0)
		System.err.println("为用户打标签成功啦");
		else System.err.println("为用户打标签出现错误！！！");
	}
	
	/**
	 * 创建标签
	 */
	public static void create(String tagName){
		AccessToken at = WeixinUtil.getAccessToken();
		//AccessToken at = WeixinUtil.getAccessToken("wxb8bf238f93411f63","907a802448889da805d709285ab35c6d");
		String url="https://api.weixin.qq.com/cgi-bin/tags/create?access_token=ACCESS_TOKEN".replace("ACCESS_TOKEN", at.getToken());
		Tag tag = new Tag();
		tag.setName(tagName);
		BaseResult result = new BaseResult();
		result.setTag(tag);
		String json = JSONObject.fromObject(result).toString();
		
		JSONObject jsonobject = WeixinUtil.httpRequest(url, "POST", json);
		System.out.println("创建tag返回的jsonobject:"+jsonobject);
		if(jsonobject!=null){
			String tagStr = jsonobject.getString("tag");
			JSONObject tagjson = JSONObject.fromObject(tagStr);
			String name = tagjson.getString("name");
			Integer tagId = tagjson.getInt("id");
			tag.setName(name);
			tag.setTagId(tagId);
			dao.add(tag);
		}
		
	}
	public static void main(String[] args) {
		TagToTeam ttt = new TagToTeam();
		List<String> openidList = new ArrayList<String>();
		openidList.add("oFwuT0a3EIj7Nx2YuZOR4kEB1QF8");
		ttt.setOpenid_list(openidList);
		ttt.setTagid(103);
		System.out.println(JSONObject.fromObject(ttt).toString());

	}
}
